/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers;

import boundaries.WeatherService;
import entities.OutdoorEvent;
import java.util.ArrayList;
import java.util.Iterator;

import javax.ejb.EJB;
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;


/**
 *
 * @author Alberto, Alessandra
 */
@Startup
@Singleton
public class WeatherScheduler {
   
    @PersistenceContext
    EntityManager em;
    
    @EJB
    WeatherManager wm;

    @Schedule()
    public void checkWeather() {
        ArrayList<OutdoorEvent> futureevents = new ArrayList<>();
        futureevents.addAll(em.createNamedQuery("findOutdoorEventInFuture" , OutdoorEvent.class)
                .getResultList());
        Iterator<OutdoorEvent> iter = futureevents.iterator();
        while (iter.hasNext()) {
            wm.retrieveWeatherForecast(iter.next());
        }
    }
    
}
